package com.douban.app.service.impl;

import com.douban.app.model.Singer;
import com.douban.app.model.Song;
import com.douban.app.service.SingerService;
import com.douban.app.service.SongService;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.data.mongodb.core.query.Query;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SingerServiceImpl implements SingerService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private SingerService singerService;
    @Autowired
    private SongService songService;


    @Override
    public Singer addSinger(Singer singer) {
        if (singer==null){
            return null;
        }
        if (singer.getGmtCreated() == null) {
            singer.setGmtCreated(LocalDateTime.now());
        }
        if (singer.getGmtModified() == null) {
            singer.setGmtModified(LocalDateTime.now());
        }

        return mongoTemplate.insert(singer);
    }

    @Override
    public Singer get(String singerId) {
        Singer byId = mongoTemplate.findById(singerId, Singer.class);
        if (byId!=null){
            return byId;
        }
        return null;
    }

    @Override
    public List<Singer> getAll() {
        return mongoTemplate.findAll(Singer.class);
    }

    @Override
    public List<Song> getSongBySingerName(String name) {
        Criteria name1 = Criteria.where("name").is(name);
        Query query = new Query(name1);
        List<Singer> singer = mongoTemplate.find(query, Singer.class);
        if (singer.size()>0) {
            Criteria singerIds = Criteria.where("singerIds").is(singer.get(0).getId());
            Query query1 = new Query(singerIds);
            List<Song> songs = mongoTemplate.find(query1, Song.class);
            return songs;
        }
        return null;
    }
    @Override
    public Singer getSingerBySingerName(String name) {
        if (name==null){
            return null;
        }
        Criteria name1 = Criteria.where("name").is(name);
        Query query = new Query(name1);
        List<Singer> singers = mongoTemplate.find(query, Singer.class);
        if (singers.size()<=0){
            return null;
        }else {
            return singers.get(0);
        }

    }
    @Override
    public boolean modify(Singer singer) {
        if (singer==null){
            return false;
        }
        Query query = new Query(Criteria.where("id").is(singer.getId()));
        Update update = new Update();
        if (singer.getAvatar()!=null){
            update.set("avatar",singer.getAvatar());
        }
        if (singer.getName()!=null){
            update.set("name",singer.getName());
        }
        if (singer.getHomepage()!=null){
            update.set("homepage",singer.getHomepage());
        }
        if (singer.getSimilarSingerIds()!=null){
            update.set("similarSingerIds",singer.getSimilarSingerIds());
        }
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Singer.class);

        return updateResult!=null||updateResult.getModifiedCount()>0;
    }

    @Override
    public boolean delete(String stringId) {
        if (StringUtils.isEmpty(stringId)){
            return false;
        }
        Singer singer = singerService.get(stringId);
        if (singer!=null){
            mongoTemplate.remove(singer);
            return true;
        }
        return false;
    }

    @Override
    public List<Song> songs(String id) {
        Criteria singerIds = Criteria.where("singerIds").is(id);
        Query query = new Query(singerIds);

        List<Song> songs = mongoTemplate.find(query, Song.class);

        return songs;
    }
}
